@echo off
chcp 65001 >nul
set PID_FILE=python_pid.txt

echo Stopping Python program...

:: 检查 PID 文件是否存在
if not exist %PID_FILE% (
    echo No PID file found. The Python program may not be running.
    pause
    exit /b
)

:: 读取 PID 文件内容
set PID=
for /f "usebackq delims=" %%i in ("%PID_FILE%") do (
    set PID=%%i
)

:: 检查 PID 是否为空
if not defined PID (
    echo PID file is empty. No process to terminate.
    del %PID_FILE%  :: 删除空的 PID 文件
    pause
    exit /b
)

:: 尝试终止进程
echo Attempting to terminate process with PID: %PID%
taskkill /f /pid %PID% >nul 2>&1
if %errorlevel% == 0 (
    echo Process %PID% terminated.
) else (
    echo Failed to terminate process %PID%. It may have already been closed.
)

:: 删除 PID 文件
del %PID_FILE%
